package com.erp.erp_mask.controller;

import com.erp.erp_mask.mapper.TOrderCommMapper;
import com.erp.erp_mask.mapper.TProductsMapper;
import com.erp.erp_mask.mapper.TSalesordersMapper;
import com.erp.erp_mask.pojo.*;
import com.erp.erp_mask.service.TOrderCommService;
import com.erp.erp_mask.service.TProductsService;
import com.erp.erp_mask.service.TSalesordersService;
import com.erp.erp_mask.vo.Result;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import net.sf.jasperreports.engine.JasperRunManager;
import org.apache.ibatis.io.Resources;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.sql.DataSource;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ord")
public class TSalesorderController {
    @Resource
    private TSalesordersService tSalesordersService;
    @Resource
    private TOrderCommMapper tOrderCommMapper;

    @GetMapping("/index")
    public ModelAndView modelAndView(){
        return new ModelAndView("/ord/salesorders");
    }

    @GetMapping("/ordsearch")
    public Result index(String kename, String saleName,String channame,Integer orderId,String orderDate,String comeTime,String orderNumber){
        return Result.success(tSalesordersService.searchPage(kename, saleName, channame, orderId,orderDate,comeTime,orderNumber)
        );
    }
    @Resource
    private DataSource dataSource;
    @SneakyThrows
    @GetMapping("/exportPdf")
    public void exportPdf( HttpServletResponse response){
        response.setContentType("application/pdf");
        InputStream in= Resources.getResourceAsStream("muban/xiaoshou.jasper");
        JasperRunManager.runReportToPdfStream(in,response.getOutputStream(),null,dataSource.getConnection());
    }

    @GetMapping("/searchx")

    public Result searchx(String number){
        System.out.println(number);
        return Result.success(tSalesordersService.searchxx(number));
    }
    @GetMapping("/chansearch")
    public Result chansearch(Integer commid){
        System.out.println(commid);
        return Result.success(tOrderCommMapper.chansearch(commid));
    }
    @PostMapping("/add")
    public Result addList(@RequestBody TOrderComm tOrderComm){
        try{
            System.out.println(tOrderComm);
            tOrderCommMapper.comminsert(tOrderComm);
            return Result.success("添加成功！");
        }catch (Exception e){
            return Result.fail("添加失败！");
        }
    }

    @DeleteMapping("/del")
    public Result del(Integer ordcomId) {
        try {
            System.out.println(ordcomId);
            tOrderCommMapper.del(ordcomId);
            return Result.success("删除成功!");
        } catch (Exception e) {
            return Result.fail("删除失败!");
        }
    }



}
